class_name HoldCompass
extends BaseHoldItem

@onready var needle: Sprite2D = $ViewRoot/Needle
@onready var view_root: Node2D = $ViewRoot


func _process(delta: float) -> void:
	process_needle_direction()
	

func process_needle_direction():
	var player = holder as Player
	if player.view_root.scale.x < 0:
		view_root.scale.x = -1
	else:
		view_root.scale.x = 1
	
	var min_distance: float = INF
	var min_distance_target: Node2D = null
	
	var level = get_tree().current_scene as Level
	var crates = level.get_all_crates()
	for crate: WoodCrate in crates:
		if crate.crate_data.is_broken: continue
		var drop_config = crate.drop_config
		if not drop_config: continue
		for drop_item_config: DropItemConfig in drop_config.drop_items_config:
			if drop_item_config.drop_item_name == "drop_big_key":
				var distance = crate.global_position.distance_to(global_position)
				if distance < min_distance:
					min_distance = distance
					min_distance_target = crate
				break
				
	if min_distance_target:
		var direction = global_position.direction_to(min_distance_target.global_position)
		needle.global_rotation = lerp(needle.global_rotation, direction.angle(), 0.05) 
	
	
